package cn.wolfcode.web.controller;

import cn.wolfcode.domain.MessageBoard;
import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.qo.MessageBoardQueryObject;
import cn.wolfcode.service.IMessageBoardService;
import cn.wolfcode.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@Controller
@RequestMapping("/messageBoard")
public class MessageBoardController {

    @Autowired
    private IMessageBoardService messageBoardService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") MessageBoardQueryObject qo) {
        PageInfo<MessageBoard> pageInfo = messageBoardService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        List<SystemDictionary> catalogs = systemDictionaryService.queryBySn("business");
        model.addAttribute("catalogs", catalogs);
        return "messageBoard/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id) {

        if (id != null) {
            messageBoardService.delete(id);
        }
        return "redirect:/messageBoard/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(MessageBoard messageBoard) {

        if (messageBoard.getId() == null) {
            messageBoardService.save(messageBoard);
        } else {
            messageBoardService.update(messageBoard);
        }
        return "redirect:/messageBoard/list";
    }



}
